package com.zhang.zhangapp.view;

import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;


import com.zhang.zhangapp.R;
import com.zhang.zhangapp.utils.ActivityManager;
import com.zhang.zhangapp.utils.NetUtils;
import com.zhang.zhangapp.utils.T;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by 罗肖 on 2017/6/29.
 */
public class BaseAppCompatActivityActivity extends AppCompatActivity {

    private SweetAlertDialog sweetAlertDialog;

    /**
     * 设置ActionBar的布局
     *
     * @param layoutId 布局Id
     */
    public void setActionBarLayout(int layoutId) {
        ActionBar actionBar = getSupportActionBar();
        if (null != actionBar) {
            actionBar.setDisplayShowHomeEnabled(false);
            actionBar.setDisplayShowCustomEnabled(true);
            actionBar.setTitle("");
//            View v = LayoutInflater.from(this).inflate(layoutId, null);
//            ActionBar.LayoutParams layout = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
            actionBar.setCustomView(layoutId);
        }
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.in_to_right, 0);
    }

    @Override
    protected void onStart() {
        super.onStart();
        ActivityManager.getActivityManager().addActivity(this);
    }

    @Override
    public void finish() {
        super.finish();
        ActivityManager.getActivityManager().removeActivity(this);
        overridePendingTransition(0, R.anim.out_to_left);
    }

    int i = -1;

    public boolean showLoadingDialog() {
        if (!NetUtils.isConnected(this)) {
            T.showShort(this, "网络异常，请检查网络连接");
            return false;
        }
        if (sweetAlertDialog != null && sweetAlertDialog.isShowing())
            return true;
        i = -1;
        sweetAlertDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE)
                .setTitleText("数据加载中...");
        sweetAlertDialog.show();
        sweetAlertDialog.setCancelable(false);
        new CountDownTimer(800 * 7, 800) {
            public void onTick(long millisUntilFinished) {
                // you can change the progress bar color by ProgressHelper every 800 millis
                i++;
                switch (i) {
                    case 0:
                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.blue_btn_bg_color));
                        break;
                    case 1:
                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_50));
                        break;
                    case 2:
                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color));
                        break;
                    case 3:
                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_20));
                        break;
                    case 4:
                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_blue_grey_80));
                        break;
                    case 5:
                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.warning_stroke_color));
                        break;
                    case 6:
                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color));
                        break;
                }
            }

            public void onFinish() {
                i = -1;
            }
        }.start();

        return true;
    }

    public boolean showLoadingDialog(String title) {
        if (!NetUtils.isConnected(this)) {
            T.showShort(this, "网络异常，请检查网络连接");
            return false;
        }
        if (sweetAlertDialog != null && sweetAlertDialog.isShowing())
            return true;
        i = -1;
        sweetAlertDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE)
                .setTitleText(title);
        sweetAlertDialog.show();
        sweetAlertDialog.setCancelable(false);
        new CountDownTimer(800 * 7, 800) {
            public void onTick(long millisUntilFinished) {
                // you can change the progress bar color by ProgressHelper every 800 millis
                i++;
                switch (i) {
                    case 0:
                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.blue_btn_bg_color));
                        break;
                    case 1:
                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_50));
                        break;
                    case 2:
                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color));
                        break;
                    case 3:
                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_deep_teal_20));
                        break;
                    case 4:
                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.material_blue_grey_80));
                        break;
                    case 5:
                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.warning_stroke_color));
                        break;
                    case 6:
                        sweetAlertDialog.getProgressHelper().setBarColor(getResources().getColor(R.color.success_stroke_color));
                        break;
                }
            }

            public void onFinish() {
                i = -1;
                sweetAlertDialog.setTitleText("Success!")
                        .setConfirmText("OK")
                        .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
            }
        }.start();

        return true;
    }

    public void dismisionDalog() {
        if (sweetAlertDialog != null)
            sweetAlertDialog.dismiss();
    }

    /** *******************************************百度地图定位相关 **************************************************/
    /** *******************************************百度地图定位相关 **************************************************/
    /**
     * ******************************************百度地图定位相关
     **************************************************/
//    protected LocationClient mLocationClient;
//
//    protected void initLocation() {
//        mLocationClient = new LocationClient(this);
//        LocationClientOption option = new LocationClientOption();
//        //就是这个方法设置为true，才能获取当前的位置信息
//        option.setIsNeedAddress(true);
//        option.setOpenGps(true);
//        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
//        );//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备
//        option.setCoorType("gcj02");//可选，默认gcj02，设置返回的定位结果坐标系
//        //int span = 1000;
//        //option.setScanSpan(span);//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的
//        mLocationClient.setLocOption(option);
//    }
}
